#ifndef TSB_CONTROLLER_DEVICE_SERVER
#define TSB_CONTROLLER_DEVICE_SERVER
/*
* 
* @license	Apache License 2.0
* @author	Eugene Kuznetsov
*
*/

#include "Base.hpp"
#include <string>
#include <vector>

#define AUTH_TIMEOUT 5
#define TARGET_SERVERS_STARTUP_TIMEOUT 10

class CDevice;
class CDataBuffer;

class CDeviceServer
{
	CDeviceServer(UInt8 port);
	~CDeviceServer();
	
	Base::eState Init();
	Base::eState AcceptConnection(Int32& client_link, std::string& ip_address, UInt8& port);
	Link::eListenState Shutdown();
	inline Link::eListenState getState() const;
	inline void setState(Link::eListenState state);
	static UInt16 getDeviceCount();

	static CDeviceServer* getInstance();

	static CDevice* RegisterDevice(Int32 client_link, std::string ip_address, UInt8 port);
	static void UnRegisterDevice(CDevice* device);

public:
	static Base::eState RunDeviceServer(UInt8 port);

private:
	struct HStarterParams
	{
		UInt8 m_nPort;
	};
	static void DeviceServerProcess(void* starter_params);
	static void DispatcherProcess(void* device);
	
private:
	Link::eListenState m_eState;
	bool m_bIsInited;
	UInt8 m_nPort;
	Int32 m_nLink;
	/*
	* @todo
	* replace variable m_nLastError with queue of errors
	*/
	Int16 m_nLastError;
	static void* m_pDeviceCriticalSection;
	static std::vector<CDevice*> m_vDevices;
	static CDeviceServer* m_pInstance;
};

#endif
